//
//  LZUserModel.m
//  laziz_Consumer
//
//  Created by ZhaoDongBo on 2016/11/10.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZUserModel.h"
#import "GVUserDefaults+LZProperties.h"
#import "NSString+Additions.h"
#import "LZNotificationPresenter.h"

@implementation LZUserModel

static LZUserModel * userModel = nil;

+ (instancetype)Instance {
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        userModel = [[self alloc] init];
    });
    return userModel;
}

- (instancetype)init {
    self = [super init];
    if (self) {
    }
    return self;
}

- (NSInteger)loginType {
    return [GVUserDefaults standardUserDefaults].loginType;
}

- (NSString *)userId {
    return [GVUserDefaults standardUserDefaults].userId;
}

- (NSString *)userToken {
    return [GVUserDefaults standardUserDefaults].userToken;
}

- (NSString *)sellerId {
    return [GVUserDefaults standardUserDefaults].sellerId;
}

//login or logout

- (BOOL)isLogin {
    return (self.userToken && [self.userToken length] > 0);
}

- (void)logOut {
    self.userInfo = nil;
    self.merchantInfo = nil;
    //因为推出后要保留用户上次的登陆类型.所以这里注释掉.不能置空登陆类型
//    [GVUserDefaults standardUserDefaults].loginType = -1;
    [GVUserDefaults standardUserDefaults].userId = nil;
    [GVUserDefaults standardUserDefaults].userToken = nil;
    [GVUserDefaults standardUserDefaults].sellerId = nil;
    [LZNotificationPresenter removeNotificationAlias];
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}

- (void)setUserInfo:(LZCurrentUserInfo *)userInfo
{
    [LZCurrentUserInfo setUserInfoWithModel:userInfo];
}

- (LZCurrentUserInfo *)userInfo {
    
    if (![self isLogin])
        return nil;
     
    return [LZCurrentUserInfo getUserInfoModel];
}

- (LZMerchantInfoModel*)merchantInfo {

    if ([GVUserDefaults standardUserDefaults].authType != kAuthType_completeAuth)
        return nil;
    
    return _merchantInfo;
}

@end
